<template>
  <div class="login-container flx-center">
    <div class="login-box">
      <div class="login-left">
        <div class="title">无线网质量评估系统</div>
        <div class="sub">实时监测，精准评估</div>
        <img src="../assets/images/4.svg" alt="" />
      </div>

      <div class="login-form">
        <div class="login-logo">
          <!-- <img class="login-icon" src="../assets/images/logo.svg" alt="" /> -->
          <p class="logo-text">欢迎登录</p>
        </div>
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules" size="large">
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username" type="text" auto-complete="off"
              placeholder="用户名">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" type="password" show-password v-model="loginForm.password" auto-complete="off"
              placeholder="密码" @keyup.enter="handleLogin">
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input :prefix-icon="CircleCheck" v-model="loginForm.code" size="large" auto-complete="off"
              placeholder="验证码" style="width: 63%" @keyup.enter="handleLogin">
              <!-- <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
-->
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item>
          <div class="flx-row">
            <el-checkbox style="flex: 1" v-model="loginForm.rememberMe" label="记住密码" size="large" />
          </div>
        </el-form>

        <div class="login-btn">
          <el-button :icon="CircleClose" round @click="resetForm()" size="large">重置</el-button>
          <el-button :icon="UserFilled" round @click.prevent="handleLogin" size="large" type="primary"
            :loading="loading">
            <span v-if="!loading">登录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </div>

      </div>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: ""
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

watch(route, (newRoute) => {
  redirect.value = newRoute.query && newRoute.query.redirect;
}, { immediate: true });

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore.login(loginForm.value).then(() => {
        router.push({ path: redirect.value || "/" });
      }).catch(() => {
        loading.value = false;
      });
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}

getCode();
getCookie();
</script>

<style lang='scss' scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  background: url('../assets/images/bg.png');
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  .login-box {
    width: 96%;
    height: 94%;
    background-color: hsla(0, 0%, 100%, 0.85);
    border-radius: 10px;
    padding: 0 8% 0 50px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-around;

    .login-left {
      width: 800px;
      color: #6E736FFF;

      .title {
        font-size: 48px;
        font-weight: 400;
        letter-spacing: 0px;
        color: rgba(47, 57, 78, 1);
        text-align: left;
      }

      img {
        width: 735px;
        height: 465px;
      }
    }

    .login-form {
      width: 400px;
      height: 460px;
      padding: 10px 40px 35px 40px;
      border-radius: 10px;
      -webkit-box-shadow: 2px 3px 7px rgb(0 0 0 / 20%);
      box-shadow: 2px 3px 7px rgb(0 0 0 / 20%);

      .login-logo {
        display: flex;
        align-items: center;
        justify-content: center;
        // margin-bottom: 45px;

        .login-icon {
          width: 60px;
          // height: 52px;
        }

        .logo-text {
          font-weight: bold;
          font-size: 32px;
          padding-left: 25px;
          white-space: nowrap;
          color: #2490fb;
        }
      }

      .el-form-item {
        margin-bottom: 20px;

      }

      .flx-row {
        span {
          font-size: 14px;
          color: #2d51e6;
        }

        .img {
          flex: 1;
          text-align: center;
        }
      }

      .login-btn {
        margin-top: 20px;
        padding-bottom: 20px;
        display: flex;
        justify-content: space-between;
        width: 100%;
        white-space: nowrap;

        .el-button {
          width: 185px;
        }
      }
    }
  }
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
